package com.ydcycloud.company.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.smart.mvc.dao.hibernate.JdbcDao;
import com.smart.mvc.model.Pagination;
import com.ydcycloud.company.dao.CompanyDao;
import com.ydcycloud.company.model.Company;
import com.ydcycloud.company.model.Trade;
import com.ydcycloud.sys.dao.impl.BaseDaoImpl;


@Component("companyDao")
public class CompanyDaoImpl extends BaseDaoImpl<Company, Integer> implements CompanyDao {

	@Resource
	private JdbcDao baseJdbcDao;

	@Override
	public Pagination<Map<String, Object>> findCompany(String companyName,
			String areaCode, String tradeName, Integer pageNo, Integer pageSize) {
		
		StringBuffer sql = new StringBuffer();
		List<Object> params = new ArrayList<Object>(); 
		sql.append("SELECT a.id,a.companyName,a.tradeId,a.personCount,a.address as areaCode,a.logoUrl, COUNT(b.companyId) AS jobCount"
				+ " FROM t_company AS a"
				+ " LEFT JOIN t_com_profession AS b ON b.companyId = a.id WHERE 1=1");
				if(companyName != null){
					sql.append(" and companyName like '%"+companyName+"%' ");
				}
				if(areaCode != null){
					sql.append(" and address = '"+areaCode+"'");
				}
				if(tradeName != null){
					sql.append(" and tradeId = '"+tradeName+"'");
				}
		sql.append(" GROUP BY a.id ");
		sql.append(" order by a.create_date desc ");
		System.out.println(sql.toString());
		Pagination<Map<String,Object>> p = new Pagination<Map<String,Object>>(pageNo,pageSize);
		baseJdbcDao.pageSqlMapQuery(p, sql.toString(), params.toArray());
		
		return p;
	}

	@Override
	public Pagination<Map<String, Object>> getCompanyList(String companyName,
			Integer pageNo, Integer pageSize) {
		StringBuffer sql = new StringBuffer();
		List<Object> params = new ArrayList<Object>(); 
		sql.append("SELECT a.id,a.addressDetail,a.companyPhone,a.companyName,c.tradeName,a.personCount,a.address as areaCode,a.logoUrl, COUNT(b.companyId) AS jobCount"
				+ " FROM t_company AS a"
				+ " LEFT JOIN t_com_profession AS b ON b.companyId = a.id"
				+ " LEFT JOIN t_trade c ON a.tradeId=c.id WHERE 1=1");
				if(companyName != null){
					sql.append(" and companyName like '%"+companyName+"%' ");
				}
		sql.append(" GROUP BY a.id ");
		sql.append(" order by a.create_date desc ");
		System.out.println(sql.toString());
		Pagination<Map<String,Object>> p = new Pagination<Map<String,Object>>(pageNo,pageSize);
		baseJdbcDao.pageSqlMapQuery(p, sql.toString(), params.toArray());
		
		return p;
	}

	@Override
	public List<Trade> findTradeName() {
		String sql = "SELECT id,tradeName FROM t_trade ";
		return findListBySql(Trade.class, sql);
	}

	

	
}
